import React from "react";
import ProForm, { ProFormText } from "@ant-design/pro-form";
import { updateUser } from '@/services/ant-design-pro/user';
import { Modal, message } from 'antd'

const Edit = (props) => {

    const { isModalEdit, isShowModalEdit, actionRef, setUserData, editData } = props;

    /**
     * 编辑用户
     * @param {obj} params 参数
     * @returns 
     */
    const editUser = async (params) => {

        for (let key in params) {
            if (params[key] == editData[key]) {
                return message.error("您还未修改数据");
            }
        }

        let result = await updateUser({
            ...editData,
            ...params
        });
        if (result.status == 'ok') {
            await setUserData();
            actionRef.current.reload();
            message.success("修改成功");
        } else {
            message.error('修改失败：' + result.data.message);
        }
        isShowModalEdit(false);
    }

    return (
        <Modal title="编辑用户"
            footer={false}
            visible={isModalEdit}
            onCancel={() => isShowModalEdit(false)}
            destroyOnClose={true}>
            <ProForm
                onFinish={(values) => editUser(values)}
                initialValues={{
                    username: editData.username,
                    email: editData.email,
                    password: editData.password
                }}
            >
                <ProFormText
                    name="username"
                    label="昵称"
                    placeholder="请输入名称"
                    rules={[
                        { required: true, message: "请输入昵称" }
                    ]}
                />
                <ProFormText
                    name="email"
                    label="邮箱"
                    placeholder="请输入邮箱"
                    rules={[
                        { required: true, message: "请输入邮箱" }
                    ]}
                />
                <ProFormText.Password
                    name="password"
                    label="密码"
                    placeholder="请输入密码"
                    rules={[
                        { required: true, message: "请输入密码" },
                        { min: 6, message: '密码最小6位' }
                    ]}
                />
            </ProForm>
        </Modal>
    );
}

export default Edit;